package com.tarena.lbs.message.web.service.impl;

import com.tarena.lbs.basic.api.UserGroupApi;
import com.tarena.lbs.common.message.constance.Const;
import com.tarena.lbs.marketing.api.MarketingApi;
import com.tarena.lbs.message.web.mapper.DynamicMsgFieldsMapper;
import com.tarena.lbs.message.web.mapper.TimingMsgTemplateMapper;
import com.tarena.lbs.message.web.service.TimingMsgTemplateService;
import com.tarena.lbs.pojo.basic.vo.UserVO;
import com.tarena.lbs.pojo.marketing.bo.MarketingActivityBO;
import com.tarena.lbs.pojo.message.dos.DynamicMsgFields;
import com.tarena.lbs.pojo.message.dos.TimingMsgTemplate;
import com.tarena.lbs.pojo.message.param.TimingMsgTemplateInsertParam;
import com.tarena.lbs.pojo.message.param.TimingMsgTemplateUpdateParam;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 定时消息模板服务实现类
 */
@Service
public class TimingMsgTemplateServiceImpl implements TimingMsgTemplateService {

    @Autowired
    TimingMsgTemplateMapper timingMsgTemplateMapper;


    @Autowired
    DynamicMsgFieldsMapper dynamicMsgFieldsMapper;

    @Autowired
    private UserGroupApi userGroupApi;

    @Resource
    private MarketingApi marketingRpcApi;

    /**
     * 新增定时消息模板
     *
     * @param timingMsgTemplateInsertParam 定时消息模板参数
     * @return 是否新增成功
     */
    @Override
    public Boolean insert(TimingMsgTemplateInsertParam timingMsgTemplateInsertParam) {
        TimingMsgTemplate timingMsgTemplate = new TimingMsgTemplate();
        BeanUtils.copyProperties(timingMsgTemplateInsertParam, timingMsgTemplate);
        int insertRows = timingMsgTemplateMapper.insert(timingMsgTemplate);
        return insertRows >= 1 ? true : false;
    }

    /**
     * 查询所有定时消息模板
     *
     * @return 定时消息模板列表
     */
    @Override
    public List<TimingMsgTemplate> selectAll() {
        List<TimingMsgTemplate> list = timingMsgTemplateMapper.selectAll();
        for (TimingMsgTemplate temp : list) {
            MarketingActivityBO marketingActivityBO = marketingRpcApi.getActivityById(temp.getActivityId());
            temp.setActivityName(marketingActivityBO.getActivityName());
            temp.setTargetCustomer(marketingActivityBO.getTargetCustomer());
        }
        return list;
    }

    /**
     * 根据ID更新定时消息模板
     *
     * @param timingMsgTemplateUpdateParam 定时消息模板参数
     * @return 是否更新成功
     */
    @Override
    public Boolean UpdateById(TimingMsgTemplateUpdateParam timingMsgTemplateUpdateParam) {
        TimingMsgTemplate timingMsgTemplate = new TimingMsgTemplate();
        BeanUtils.copyProperties(timingMsgTemplateUpdateParam, timingMsgTemplate);
        int row = timingMsgTemplateMapper.updateByPrimaryKeySelective(timingMsgTemplate);
        return row >= 1 ? true : false;
    }

    /**
     * 根据ID删除定时消息模板
     *
     * @param id 定时消息模板ID
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteById(Long id) {
        int row = timingMsgTemplateMapper.deleteByPrimaryKey(id);
        return row >= 1 ? true : false;
    }

    @Override
    public TimingMsgTemplate getMsgContent(Integer id,Integer userId) {
        TimingMsgTemplate timingMsgTemplate = timingMsgTemplateMapper.selectByPrimaryKey(id);
        List<DynamicMsgFields> dynamicMsgFieldList = dynamicMsgFieldsMapper.selectByTempId(id);
        for (DynamicMsgFields temp : dynamicMsgFieldList) {
            if (Const.USER_NAME.equals(temp.getFieldName())) {
                UserVO userVO = userGroupApi.getUser(userId);
                if(timingMsgTemplate.getContent().contains(Const.USER_NAME)) {
                    timingMsgTemplate.setContent(timingMsgTemplate.getContent().replace(Const.USER_NAME, userVO.getUserName()));
                }
                if(timingMsgTemplate.getMsgTitle().contains(Const.USER_NAME)) {
                    timingMsgTemplate.setMsgTitle(timingMsgTemplate.getMsgTitle().replace(Const.USER_NAME, userVO.getUserName()));
                }
            }
        }
        return timingMsgTemplate;
    }
}
